package cn.wolfcode.wolf2w.redis.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVoRedisService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import cn.wolfcode.wolf2w.util.RedisKeys;
import cn.wolfcode.wolf2w.vo.StrategyStatisVO;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVoRedisServiceImpl implements IStrategyStatisVoRedisService {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private StringRedisTemplate template;

    @Override
    public void increaseViewnum(String id) {
        // 获取 攻略统计对象
        StrategyStatisVO vo = this.getStrategyStaticVO(id);
        // 设值
        vo.setViewnum(vo.getViewnum() + 1);
        // 更新
        this.setStrategyStaticVO(vo);
    }

    @Override
    public StrategyStatisVO getStrategyStaticVO(String sid) {
        // 设计key
        String vokey = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        StrategyStatisVO vo = null;
        // 判断这个key 是否存在 redis中
        if (!template.hasKey(vokey)){    // 如果不存在，初始化，通过id去mongodb中获取 strategy对象
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(strategy.getId());
            template.opsForValue().set(vokey,JSON.toJSONString(vo));
        }else {             // 如果存在，直接拿
            String s = template.opsForValue().get(vokey);
            vo = JSON.parseObject(s, StrategyStatisVO.class);
        }
        return vo;
    }

    @Override
    public void setStrategyStaticVO(StrategyStatisVO vo) {
        // 设计key
        String vokey = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        // 更新
        template.opsForValue().set(vokey,JSON.toJSONString(vo));
    }

    @Override
    public void increaseReplynum(String sid) {
        StrategyStatisVO vo = this.getStrategyStaticVO(sid);
        vo.setReplynum(vo.getReplynum() + 1);
        this.setStrategyStaticVO(vo);
    }

    @Override
    public boolean increaseFavor(String sid, String uid) {
        boolean flag = true;
        // 设计key
        String key = RedisKeys.STRATEGY_STATIS_FAVORNUM.join(uid);
        // 判断key 是否存在
        List<String> sidList = new ArrayList<>();
        if (template.hasKey(key)){
            // 如果存在，直接拿
            String s = template.opsForValue().get(key);
            sidList = JSON.parseArray(s,String.class);
        }
        StrategyStatisVO vo = this.getStrategyStaticVO(sid);
        // 判断 sid 是否存在 SidList 集合
        if (sidList.contains(sid)){  // 如果存在，取消
            vo.setFavornum(vo.getFavornum() - 1);
            // 移除
            sidList.remove(sid);
            flag = false;
        }else {  // 如果不存在，收藏
            vo.setFavornum(vo.getFavornum() + 1);
            // 加入
            sidList.add(sid);
        }
        template.opsForValue().set(key,JSON.toJSONString(sidList));
        // 更新
        this.setStrategyStaticVO(vo);
        return flag;
        /*return sidList.contains(sid);*/
    }

    @Override
    public boolean increaseThumb(String sid, String uid) {
        // 设计 key
        String key = RedisKeys.STRATEGY_STATIS_THUMBNUM.join(uid,sid);
        // 判断 key 是否存在
        if (template.hasKey(key)){      // 今天已经置顶过了
            return false;
        }
        // 如果不存在，可以置顶，设置有效时间，存入Redis中，获取vo对象，加一
        Date now = new Date();
        Date end = DateUtil.getEndDate(now);
        // 置顶时间到今天最后一秒的时间
        long between = DateUtil.getDateBetween(end, now);
        StrategyStatisVO vo = this.getStrategyStaticVO(sid);
        vo.setThumbsupnum(vo.getThumbsupnum() + 1);
        template.opsForValue().set(key,"1",between +1, TimeUnit.SECONDS);
        this.setStrategyStaticVO(vo);
        return true;
    }

    @Override
    public boolean isVoExists(String sid) {
        /*StrategyStatisVO vo = this.getStrategyStaticVO(sid);
        if (vo == null){
            return false;
        }*/
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        return template.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> getVOListByPattern(String pattern) {
        // keys strategy_statis_vo:*
        Set<String> keys = template.keys(pattern);
        List<StrategyStatisVO> vos = new ArrayList<>();
        if (keys != null && keys.size() > 0){
            for (String key : keys) {       // 循环 keys,每个key去查询 vo 对象，添加到 vo集合中
                String s = template.opsForValue().get(key);
                vos.add(JSON.parseObject(s,StrategyStatisVO.class));
            }
        }
        return vos;
    }
}
